package c2.group.business.gradepvp;

import c2.advanced.business.base.KvData;
import cate.common.table.d.GDGradePvp;
import cate.game.role.RoleSimpleBase;
import cate.game.role.pvp.gradepvp.GradePvpPlayer;
import cate.game.role.pvp.gradepvp.GradePvpRecord;
import easy.java.dev.note.NoteField;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GradePvpKv extends KvData {
    @NoteField(value = "当前赛季")
    public int season;

    @NoteField(value = "当前赛季的状态")
    public int period;

    @NoteField(value = "功能开启时间")
    public long startTime;

    @NoteField(value = "六神")
    public Map<Integer, List<RoleSimpleBase>> seasonSixGods;

    @NoteField("大神风采")
    public List<GradePvpRecord> newBeeRecords;

    @Override
    public void init(String key) {
        super.init(key);
        if (seasonSixGods == null) {
            seasonSixGods = new HashMap<>();
        }
        if (newBeeRecords == null) {
            newBeeRecords = new ArrayList<>();
        }
        if (startTime == 0) {
            startTime = GDGradePvp.START_TIME;
        }
    }

    public synchronized void updateSixGod(List<GradePvpPlayer> players) {
        List<RoleSimpleBase> list = new ArrayList<>();
        for (GradePvpPlayer player : players) {
            list.add(player.ps.snapshot.base.copy());
        }
        seasonSixGods.put(season, list);
    }

    public synchronized List<RoleSimpleBase> loadSixGods(int season) {
        return seasonSixGods.getOrDefault(season, new ArrayList<>());
    }

    public synchronized void addRecords(GradePvpRecord record) {
        if (record.starA >= GDGradePvp.GOD_GRADE_STAR_MIN
                || record.starB >= GDGradePvp.GOD_GRADE_STAR_MIN) {
            newBeeRecords.add(record);
            if (newBeeRecords.size() >= GDGradePvp.GOD_RECORD_NUM_MAX) {
                newBeeRecords.remove(0);
            }
        }
    }

    public synchronized List<GradePvpRecord> loadNewBeeRecords() {
        return newBeeRecords;
    }
}
